﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CLVisionTcpIP;
using static CLVisionTcpIP.CLTcpServer;

namespace Demo
{
    public partial class TcpSeverDemo : Form
    {
        public TcpSeverDemo()
        {
            InitializeComponent();
        }
        CLTcpServer tcpSever = new CLTcpServer();
        private void button1_Click(object sender, EventArgs e)
        {

            tcpSever.OnMessageReceived = (msg) =>
            {
                this.Invoke(new Action(() =>
                {
                    listBox1.Items.Add(msg);
                }));
            };
            string IP = textBox2.Text;
            int port = int.Parse( textBox3.Text);
            tcpSever.Start(out int ErrorCode,out string ErrorText,IP, port);
           // tcpSever.Stop();
            //tcpSever.Start(out  ErrorCode, out  ErrorText, IP, port);
            tcpSever.OnClientConnected += (ss) =>
            {

                this.Invoke(new Action(() =>
                {

                    comboBox1.Items.Add(ss);

                }));

            };
            tcpSever.OnClientDisConnected += (ss) =>
            {

                this.Invoke(new Action(() =>
                {

                    comboBox1.Items.Remove(ss);

                }));

            };

        }

        private void button3_Click(object sender, EventArgs e)
        {
            TcpClientDemo tcpClient = new TcpClientDemo();
            tcpClient.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //string StrText  = textBox1.ToString();
            //tcpSever.SendToAll(StrText,out int ErrorCode,out string ErrorText);
            //List< ClientMessage>  meg= tcpSever.GetAllLastMessages();
            List<ClientMessage> messages = new List<ClientMessage>();
            tcpSever.GetAllLastMessages(messages, out int a,out string b);
            //string test = comboBox1.Text;
            //tcpSever.SendToClient(test, StrText);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            tcpSever.Stop();
        }
    }
}
